/*
 *  ------MakeSuremote starting program------
 *
 *  Explanation: Send basic parameters through the corresponding 
 *  XBee module.
 *  For other communication modules please use on line code generator.
 *
 * MakeSure.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version:                0.1
 *  Design:                 David Gascón
 *  Implementation:         Yuri Carmona
 */

// Include libraries
#include <MakeSureXBee868.h>
#include <MakeSureFrame.h>


// Declare global variables
packetXBee* packet;
char macHigh[10];
char macLow[11];


void setup()
{
  /////////////////////////////////
  // 1. set up the XBee module
  /////////////////////////////////
  xbee868.ON();
  
  /////////////////////////////////
  // 2. Set up RTC and ACC
  /////////////////////////////////
  delay(500);
  RTC.ON();
  ACC.ON();   

  /////////////////////////////////
  // 3. LEDs management 
  /////////////////////////////////  
  Utils.setLED(LED0, LED_ON);
  Utils.setLED(LED1, LED_ON);
  delay(2000);
  Utils.setLED(LED0, LED_OFF);
  Utils.setLED(LED1, LED_OFF);
  for (int i=0;i<24;i++)
  {
    Utils.blinkLEDs(125);
  }

  /////////////////////////////////
  // 4. Get the XBee MAC address
  /////////////////////////////////  
  xbee868.ON();
  delay(1000);  
  xbee868.flush();

  // Get the XBee MAC address
  int counter = 0;
  while((xbee868.getOwnMac()!=0) && (counter<12))
  {
    xbee868.getOwnMac();
    counter++;
  }

  // convert mac address from array to string
  Utils.hex2str(xbee868.sourceMacHigh, macHigh, 4);
  Utils.hex2str(xbee868.sourceMacLow,  macLow,  4);  


  /////////////////////////////////
  // 5. Print XBee module information
  /////////////////////////////////     
  //USB.ON();
  //USB.print("mac address:");
  //USB.print(macHigh);
  //USB.println(macLow);
  //USB.OFF();
}


void loop()
{
  ////////////////////////////////////////////////
  // 6. Message composition
  ////////////////////////////////////////////////
  
  // 6.1 Create new frame (No mote id)
  frame.createFrame(ASCII,"");  
  
  // 6.2 Add frame fields
  frame.addSensor(SENSOR_MAC, macLow); 
  frame.addSensor(SENSOR_ACC, ACC.getX(), ACC.getY(), ACC.getZ() );
  frame.addSensor(SENSOR_IN_TEMP, RTC.getTemperature());
  frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel());
  
  // 6.3 Print frame
  // Example:<=>#35690399##5#MAC:4066EF6B#ACC:-47;-26;1000#IN_TEMP:26.25#BAT:59#
  frame.showFrame();
  
  ////////////////////////////////////////////////
  // 7. Send the packet
  ////////////////////////////////////////////////
  
  // 7.1 set packet to send
  packet=(packetXBee*) calloc(1,sizeof(packetXBee)); // memory allocation
  packet->mode=BROADCAST; // set Unicast mode
  
  // 7.2 send the packet via the correct object depending on the protocol  
  // turn XBee on
  xbee868.ON();  
  // sets Destination parameters
  xbee868.setDestinationParams(packet, "000000000000FFFF", frame.buffer, frame.length); 
  // send data
  xbee868.sendXBee(packet);
    
  // check TX flag
  if( xbee868.error_TX == 0 )
  {
    //USB.println(F("sending ok"));
  }
  else 
  {
    //USB.println(F("sending error"));
  }    
   
  // 7.3 free memory
  free(packet);
  packet = NULL;
  
  delay(1000);
}
